/*
 * 컴파일을 위해 올려 놓은 소스 입니다.
 * 서버에 반영하지 마시고 다른국가에 적용할 경우 서버에 있는 class 를 카피해서 사용하십시오.
 * 
 */
package com.pushmsg.otaconf;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import java.util.*;

public class wbXML extends DefaultHandler
{
  OtaWBXML	codeTable = new OtaWBXML();
  byte[] buffer = new byte[1000];
  int index=0;
  Stack subElement = new Stack(); 
  Stack Element = new Stack(); 
  String filename;
  String strTable=null;
  private String urlsuffix=null;
  
  public wbXML(String fn, String suffix)
  {  		
  		filename = fn;
  		buffer[index++]=0x01;//Version-WBXML1.1
  		buffer[index++]=0x01;//Unknown public identifier
  		buffer[index++]=0x6A;//charset
  		buffer[index++]=0x00;//string table length
  		urlsuffix=suffix;
  }
  
  public int getLength()
  {
  	return index;
  }
  
  public byte[] getBody()
  {
  	return buffer;
  }
  
  private void setStringTableLen()
  {  	
	buffer[3]=(byte)(strTable.length()+1);
  }
  
  private void setStringTable()
  {
	String params = "free_ger|111111|t-mobile|016090509289";
	int j = params.length() % 8; 
	if(j != 0) 
	{
		for(int  k = 0; k < 8 - j;k++)
			params += " ";
	}
	//strTable = DES3Util.EncryptParam3DES(params);
	System.out.println("***********************");
	System.out.println(strTable);
	System.out.println("***********************");
	setStringTableLen();	
	byte[] tbuf = strTable.getBytes();
	for(int i=0; i<strTable.length(); i++)
		buffer[index++]=tbuf[i];
	buffer[index++]=0x00;
  }
  
  public boolean process()
  {
	SAXParserFactory   f = SAXParserFactory.newInstance();
	SAXParser          p = null;
	DefaultHandler     h = this;
	
	//setStringTable();
	try
	{
	   	p = f.newSAXParser();
	   
		InputSource inputsource = new InputSource(filename);
	  	p.parse(inputsource, h);
	  	return true;
	 } 
	catch(Throwable x)
	{
	  x.printStackTrace();
	  return false;
	}
  }

  public void startDocument()	throws SAXException
  {
  }

  public void startElement(String ns, String sName, String qName, Attributes a)	throws SAXException
  {
	Integer ndx=null;
	
	if(subElement.empty()==false)
		ndx = (Integer)subElement.peek();
	
	if(ndx!=null) 
	{
		buffer[ndx.intValue()] = (byte)((int)buffer[ndx.intValue()]|(int)codeTable.getValue("CONTENT"));
		subElement.pop();
	}
	subElement.push(new Integer(index));
	
	String element = sName!=""?sName:qName;

	if(a.getLength()!=0)
	{
		buffer[index++] = (byte)((int)codeTable.getValue(element)|(int)codeTable.getValue("ATTRIBUTE"));
		showAttributes(a);
		buffer[index++] = codeTable.getValue("END");
	
		Properties p=null;
		if(Element.empty()==false)
		{
			p=(Properties)Element.pop();
			Enumeration e = p.keys();
			String key = (String)e.nextElement();
			p.setProperty(key, "1");
			Element.push(p);
		}
		p = new Properties();
		p.setProperty(element, "0");
		Element.push(p);
	}
	else
		buffer[index++] = codeTable.getValue(element);
  }

  void showAttributes(Attributes a)	throws SAXException
  {
	String attr;
	byte code;
	boolean typeurl=false;
	if (a!=null)
	{
	  for(int j=0;j<a.getLength();j++)
	  {
		attr = a.getLocalName(j)!=""?a.getLocalName(j):a.getQName(j);
		code=codeTable.getValue(attr+"_" + a.getValue(j));
		if(code==(byte)0x07) // TYPE="URL" or NAME="URL"--For bookmark
			typeurl=true;
		if((attr.equals("VALUE"))&&(code==(byte)0xFF))
		{
			int ndx=0;
			byte[] inlinestr = new byte[a.getValue(j).getBytes().length+urlsuffix.getBytes().length+3];
			buffer[index++] = codeTable.getValue("VALUE");
			buffer[index++] = codeTable.getValue("INLINESTRING");
			for(int i=0; i<a.getValue(j).getBytes().length; i++)
				buffer[index++] = a.getValue(j).getBytes()[i];
			if(typeurl)
			{
				for(int i=0; i<urlsuffix.getBytes().length; i++)
					buffer[index++] = urlsuffix.getBytes()[i];
			}
			buffer[index++] = codeTable.getValue("ENDINLINESTR");
			/*if(typeurl==true)
			{
				buffer[index++]=(byte)0x83;//String table reference follows
				buffer[index++]=(byte)0x00;//String table index
			}*/
		}
		else
		{
			buffer[index++]=code;
		}
	  }
	}
  }

  public void characters(char[] buf, int offset, int length)	throws SAXException
  {
  }

  public void endElement(String ns, String lName, String qName)	throws SAXException
  {
	String endEle = lName!=""?lName:qName;
	if(subElement.empty()==false)
		subElement.pop();
	if(Element.empty()== false)
	{
		Properties p = (Properties)Element.peek();
		Enumeration e = p.keys();
		String key = (String)e.nextElement();
		if(p.getProperty(key).equals("1"))
			buffer[index++]=codeTable.getValue("END");
		else
			Element.pop();
			
	}
	else
		buffer[index++]=codeTable.getValue("END");
  }

  public void endDocument()	throws SAXException
  {
  }

  private String bytesToHex(byte[] data) {
	  StringBuffer buf = new StringBuffer();
	  for (int i = 0; i < data.length; i++) {
		  buf.append(byteToHex(data[i]).toUpperCase());
	  }
	  return (buf.toString());
  }
  private String byteToHex(byte data) {
	  StringBuffer buf = new StringBuffer();
	  buf.append(toHexChar((data >>> 4) & 0x0F));
	  buf.append(toHexChar(data & 0x0F));
	  return buf.toString();
  }

  private char toHexChar(int i) {
	  if ((0 <= i) && (i <= 9)) {
		  return (char) ('0' + i);
	  }
	  else {
		  return (char) ('a' + (i - 10));
	  }
  }
  public int length() {
  	return index;
  }  
}
